<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the Session_Store_Files class.
 *
 * PHP version 5
 *
 * This file is part of Majic - A rapid-development web application framework
 * for PHP 5.
 *
 * Majic is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * Majic is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Majic. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     Session
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2007-2008 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: Files.php 34 2008-07-19 04:50:53Z mjijackson $
 */

/**
 * Emulates the default PHP "files" save handler.
 *
 * @package     Session
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
class Session_Store_Files extends Session_Store
{

    /**
     * @see     Session_Store::open()
     */
    public function open($save_path, $name)
    {
        return true;
    }

    /**
     * @see     Session_Store::close()
     */
    public function close()
    {
        return true;
    }

    /**
     * @see     Session_Store::read()
     */
    public function read($id)
    {
        $file = $this->toFileName($id);
        $get = file_get_contents($file);
        return $get ? $get : '';
    }

    /**
     * @see     Session_Store::write()
     */
    public function write($id, $data)
    {
        $file = $this->toFileName($id);
        $put = file_put_contents($file, $data);
        return $put !== false;
    }

    /**
     * @see     Session_Store::destroy()
     */
    public function destroy($id)
    {
        $file = $this->toFileName($id);
        return @unlink($file);
    }

    /**
     * @see     Session_Store::gc()
     */
    public function gc($maxlifetime)
    {
        $path = ini_get('session.save_path');
        $iter = new DirectoryIterator($path);

        foreach ($iter as $file) {
            if ($file->isFile()
                && strpos($file->getFilename(), 'sess_') === 0
                && $file->getATime() < time() - $maxlifetime) {
                    @unlink("$path/$file");
            }
        }

        return true;
    }

    /**
     * Translates a session id to the name of the corresponding session file.
     *
     * @param   string      $id         The session id
     * @return  string                  The session file name for the given id
     * @access  private
     */
    private function toFileName($id)
    {
        return ini_get('session.save_path') . '/sess_' . $id;
    }

}

?>
